OpenRoads Designer CONNECT Edition SDK Help

Annotate slope (%) for all triangle center

The below code shows how to get the terrain triangles and annotate slope for all triangle centers.


//Required References
using System;
using System.Collections;
using System.Diagnostics;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using Bentley.TerrainModelNET;


 public void AnnotateSlopeForAllTriangleCenters()
        {
            try
            {
                Bentley.CifNET.GeometryModel.SDK.TerrainSurface terrainSurface = null;

                //Get active DGN file
                Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                //Get active dgn model
                Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                //Create connection to dgnModel
                Bentley.CifNET.SDK.ConsensusConnection consensusConnection = new ConsensusConnection(dgnModel);

                //Get or create text style
                DgnTextStyle textStyle = DgnTextStyle.GetByName("Line Length Label", dgnFile);
                if (null == textStyle)
                {
                    textStyle = new DgnTextStyle("Line Length Label", dgnFile);
                    textStyle.SetProperty(TextStyleProperty.Width, 400D);
                    textStyle.SetProperty(TextStyleProperty.Height, 400D);
                    textStyle.Add(dgnFile);
                }

                //Get active geometric model
                Bentley.CifNET.GeometryModel.SDK.GeometricModel geometricModel = consensusConnection.GetActiveGeometricModel();
                if (geometricModel.ActiveSurface == null) return;

                //Get active terrain
                terrainSurface = geometricModel.ActiveSurface as TerrainSurface;
                if (terrainSurface == null) return;

                ArrayList triangles = new ArrayList();

                //Browse terrain triangles
                bool processPointsFeatures(DTMDynamicFeatureInfo featureInfo, object oArg)
                {
                    if (oArg != null)
                    {
                        ArrayList al = oArg as ArrayList;
                        Bentley.CifNET.LinearGeometry.ClosedLineString cls = new Bentley.CifNET.LinearGeometry.ClosedLineString(featureInfo.FeaturePoints);
                        al.Add(cls);
                    }
                    return true;
                }

                DynamicFeaturesBrowsingDelegate hdl2P = new DynamicFeaturesBrowsingDelegate(processPointsFeatures);
                ArrayList tTriangle = new ArrayList();

                TrianglesBrowsingCriteria criteria = new TrianglesBrowsingCriteria();
                terrainSurface.DTM.BrowseTriangles(criteria, hdl2P, tTriangle);

                DTM dtm = terrainSurface.DTM;
               
                Bentley.GeometryNET.DPoint3d[] tPoints = new Bentley.GeometryNET.DPoint3d[3];
                foreach (Bentley.CifNET.LinearGeometry.ClosedLineString cls in tTriangle)
                {
                    /// Get vertices
                    tPoints = cls.GetVertices();

                    DPoint3d point1 = new DPoint3d(tPoints[0]);
                    DPoint3d point2 = new DPoint3d(tPoints[1]);
                    DPoint3d point3 = new DPoint3d(tPoints[2]);

                    //Calculate triangle center
                    DPoint3d centre = new DPoint3d((point1.X + point2.X + point3.X) / 3, (point1.Y + point2.Y + point3.Y) / 3, (point1.Z + point2.Z + point3.Z) / 3);

                    //Calculate slope of center point
                    Bentley.TerrainModelNET.DTMDrapedPoint drappedPoint = dtm.DrapePoint(centre);
                    if (null == drappedPoint || (int)Bentley.TerrainModelNET.DTMDrapedPointCode.External == drappedPoint.Code)
                    {
                        continue;
                    }

                    double slope = drappedPoint.Slope;
                    string slopePercent = slope.ToString("N2") + "%";
                    
                    //create annotation and add to model
                    TextBlock textBlockCtr = new TextBlock(textStyle, dgnModel);
                    textBlockCtr.AppendText(slopePercent);
                    textBlockCtr.SetUserOrigin(centre);
                    Bentley.DgnPlatformNET.Elements.TextElement textElementCtr = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlockCtr);
                    textElementCtr.AddToModel();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }